package com.xmmj.baselibrary.utils

import android.content.Context
import android.content.SharedPreferences
import com.alibaba.fastjson.JSON
import java.util.HashSet

/**
 * SharedPreferences工具类
 */
class SP(context: Context) {
    private val mSP: SharedPreferences

    companion object {
        private const val DBName = "com.xm"
    }

    init {
        mSP = context.getSharedPreferences(DBName, Context.MODE_PRIVATE)
    }

    fun getString(key: String?): String? {
        return mSP.getString(key, null)
    }

    fun getString(key: String?, defValue: String?): String? {
        return mSP.getString(key, defValue)
    }

    fun putString(key: String?, value: String?) {
        val editor = mSP.edit()
        editor.putString(key, value)
        editor.apply()
    }

    fun getBoolean(key: String?): Boolean {
        return mSP.getBoolean(key, false)
    }

    fun getBoolean(key: String?, defValue: Boolean): Boolean {
        return mSP.getBoolean(key, defValue)
    }

    fun putBoolean(key: String?, value: Boolean?) {
        val editor = mSP.edit()
        editor.putBoolean(key, value!!)
        editor.apply()
    }

    fun getFloat(key: String?): Float {
        return mSP.getFloat(key, 0f)
    }

    fun putFloat(key: String?, value: Float?) {
        val editor = mSP.edit()
        editor.putFloat(key, value!!)
        editor.apply()
    }

    fun getInt(key: String?, defaultValue: Int): Int {
        return mSP.getInt(key, defaultValue)
    }

    fun getInt(key: String?): Int {
        return getInt(key, 0)
    }

    fun putInt(key: String?, value: Int?) {
        val editor = mSP.edit()
        editor.putInt(key, value!!)
        editor.apply()
    }

    fun getLong(key: String?): Long {
        return mSP.getLong(key, 0)
    }

    fun putLong(key: String?, value: Long?) {
        val editor = mSP.edit()
        editor.putLong(key, value!!)
        editor.apply()
    }

    fun getStringSet(key: String?): Set<String>? {
        return mSP.getStringSet(key, HashSet())
    }

    fun putStringSet(key: String?, value: Set<String?>?) {
        val editor = mSP.edit()
        editor.putStringSet(key, value)
        editor.apply()
    }

    fun putObject(key: String?, value: Any?) {
        val editor = mSP.edit()
        editor.putString(key, if (value == null) null else JSON.toJSONString(value))
        editor.apply()
    }

    fun <T> getObject(key: String?, clazz: Class<T>?): T? {
        val value = mSP.getString(key, null)
        return if (null != value) {
            JSON.parseObject(value, clazz)
        } else null
    }

    fun removeObject(key: String?) {
        val editor = mSP.edit()
        editor.remove(key)
        editor.apply()
    }

    fun putList(key: String?, value: List<*>?) {
        val editor = mSP.edit()
        editor.putString(key, if (value == null) "" else JSON.toJSONString(value))
        editor.apply()
    }

    fun <T> getList(key: String?, clazz: Class<T>?): List<T>? {
        val value = mSP.getString(key, "")
        return if (null != value) {
            JSON.parseArray(value, clazz)
        } else null
    }
}